﻿package {
	import flash.events.*;
	import flash.display.*;
	import flash.net.*;
	public class Main extends MovieClip {

		private var sharedData:*;
		private var loader:Loader = new Loader();

		public function Main() {
			var req:URLRequest = new URLRequest("sub.swf");
			loader.contentLoaderInfo.sharedEvents.addEventListener("SUB_EVENT", onEvent);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			loader.load(req);
			addChild(loader);

			var sprite:Sprite = new Sprite();
			sprite.graphics.beginFill(0xf5f5f5);
			sprite.graphics.drawRect(0, 0, 100, 100);
			sprite.graphics.endFill();

			var xml:XML = new XML("<root><node>label</node></root>");

			var array:Array = ["item1","item2","item3"];

			var string:String = "this is a string";

			sharedData = [stage, this];
		}
		//加载完成后发送共享事件
		private function onComplete(e:Event):void {
			var sharedEvents:SharedEvents = new SharedEvents("MAIN_EVENT", true, true, sharedData);
			loader.contentLoaderInfo.sharedEvents.dispatchEvent(sharedEvents);
		}

		private function onEvent(e:SharedEvents):void {
			O.o(e.data);
			var arr:Array = e.data as Array;
			var myStage:Stage = arr[0] as Stage;
			O.o(myStage.stageWidth);
			var object:Object = arr[1];
			O.o(object.loader);
		}

		private function onError(e:Event):void {
			O.o(e);
		}
	}
}